package com.example.administrator.managesystem.activity.login;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.activity.my_profile.CertifyDataActivity;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.presenter.login.LoginPresenter;
import com.example.administrator.managesystem.ui.LoginView;
import com.example.administrator.managesystem.utils.ConstantUtil;
import com.example.administrator.managesystem.utils.ToastUtils;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;

public class RegisterActivity extends BaseActivity implements LoginView {
    private TextView object_tv,id_tv,code_tv,submit_tv,addr_tv;
    private EditText name,phone,addr_detail,verify_code;
    private RelativeLayout show_type_rl,addr_rl;
    private LinearLayout arrow_up;
    private CountDownTimer timer;
    private  static String province_selected="四川",
            city_seleted="成都",
            area_seleted="双流区";
    private CityPickerView mPicker;
    private LoginPresenter loginPresenter;
    private RadioGroup radioGroup;
    private String type="f";
    private boolean isflag=true;

//    d企业  e合作社  f个体农户
    @Override
    public void initView() {
        ConstantUtil.IMGURL=null;
        showfloatButton(false,false);
        object_tv=findViewById(R.id.manage_object);
        id_tv=findViewById(R.id.id_tv);
        code_tv=findViewById(R.id.register_code_tv);
        submit_tv=findViewById(R.id.register_send);
        addr_tv=findViewById(R.id.addr_tv);
        name=findViewById(R.id.name);
        phone=findViewById(R.id.phone);
        addr_detail=findViewById(R.id.addr_detail);
        addr_rl=findViewById(R.id.addr_rl);
        show_type_rl=findViewById(R.id.show_type_rl);
        arrow_up=findViewById(R.id.arrow_up);
        verify_code=findViewById(R.id.register_code);
        radioGroup=findViewById(R.id.type_rg);
//申明对象
         mPicker=new CityPickerView();
        //预先加载仿iOS滚轮实现的全部数据
         mPicker.init(this);
         loginPresenter=new LoginPresenter(RegisterActivity.this);
    }

    @Override
    public void doBusiness() {
    object_tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isflag){
                setDrawableRight(object_tv,R.mipmap.triangle_af);
                show_type_rl.setVisibility(View.VISIBLE);
                isflag=false;
            }else{
                setDrawableRight(object_tv,R.mipmap.triangle_be);
                show_type_rl.setVisibility(View.GONE);
                isflag=true;
            }


        }
    });

    arrow_up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            show_type_rl.setVisibility(View.GONE);
            setDrawableRight(object_tv,R.mipmap.triangle_be);
        }
    });

    id_tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoActivity(CertifyDataActivity.class,false);

        }
    });
    code_tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loginPresenter.CodeRequest(phone.getText().toString(),RegisterActivity.this);
        }
    });
    addr_rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectAddress();
        }
    });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.individual){
                    type="f";
                    object_tv.setText("个体生产经营");
                }else if(checkedId==R.id.community){
                    type="e";
                    object_tv.setText("合作社生产经营");
                }else if(checkedId==R.id.company){
                    type="d";
                    object_tv.setText("企业生产经营");
                }
            }
        });

        submit_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(object_tv.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请选择生产经营主体!");
                    return;
                }
                if(name.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请输入真实姓名!");
                    return;
                }

                if(ConstantUtil.IMGURL==null){
                    ToastUtils.showToast(RegisterActivity.this,"请上传身份证!");
                    return;
                }
                if(phone.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请输入手机号码!");
                    return;
                }
                if(verify_code.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请输入验证码!");
                    return;
                }
                if(addr_tv.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请选择省市区!");
                    return;
                }
                if(addr_detail.getText().toString().isEmpty()){
                    ToastUtils.showToast(RegisterActivity.this,"请输入详细家庭地址!");
                    return;
                }
                showProgressDialog();
                loginPresenter.registerRequest(type,name.getText().toString(),ConstantUtil.IMGURL,
                        phone.getText().toString(),verify_code.getText().toString(),province_selected,city_seleted,area_seleted,addr_detail.getText().toString(),RegisterActivity.this);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    private void setDrawableRight(TextView attention, int drawableId) {
        Drawable drawable = getResources().getDrawable(drawableId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        attention.setCompoundDrawables(null, null, drawable, null);
    }


    @Override
    public void showMessage(String msg) {
        dismissProgressDialog();
    }

    @Override
    public void navigateToHome() {
        dismissProgressDialog();

        finish();
    }

    @Override
    public void startCountDown() {
        timer= new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                code_tv.setText(millisUntilFinished / 1000 + "s");
                code_tv.setTextColor(getResources().getColor(R.color.white));
                code_tv.setEnabled(false);//设置不可点击
            }

            @Override
            public void onFinish() {
                code_tv.setText("重新获取");
                code_tv.setTextColor(getResources().getColor(R.color.white));
                code_tv.setEnabled(true);
            }
        };

        timer.start();
    }

    private void selectAddress() {
        //添加默认的配置，不需要自己定义，当然也可以自定义相关熟悉，详细属性请看demo
        CityConfig cityConfig = new CityConfig.Builder()
                . title("选择您的地址")//标题
                .titleTextSize(16)//标题文字大小
                .titleTextColor("#333333")//标题文字颜色
                .titleBackgroundColor("#FFFFFF")//标题栏背景色
                .confirTextColor("#333333")//确认按钮文字颜色
                .confirmText("确定")//确认按钮文字
                .confirmTextSize(14)//确认按钮文字大小
                .cancelTextColor("#333333")//取消按钮文字颜色
                .cancelText("取消")//取消按钮文字
                .cancelTextSize(14)//取消按钮文字大小
                .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)//显示类，只显示省份一级，显示省市两级还是显示省市区三级
                .showBackground(true)//是否显示半透明背景
                .visibleItemsCount(5)//显示item的数量
                .province("四川省")//默认显示的省份
                .city("成都市")//默认显示省份下面的城市
                .district("双流区")//默认显示省市下面的区县数据
                .provinceCyclic(true)//省份滚轮是否可以循环滚动
                .cityCyclic(true)//城市滚轮是否可以循环滚动
                .districtCyclic(true)//区县滚轮是否循环滚动
                .drawShadows(true)//滚轮不显示模糊效果
                .setLineColor("#fce600")//中间横线的颜色
                .setShowGAT(true)//是否显示港澳台数据，默认不显示
                .build();
        mPicker.setConfig(cityConfig);


//监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                addr_tv.setText(province+"/"+city+"/"+district+"");
                province_selected=String.valueOf(province);
                city_seleted=String.valueOf(city);
                area_seleted= String.valueOf(district);
                //省份province
                //城市city
                //地区district
            }

            @Override
            public void onCancel() {
                //mPicker.hide();
            }
        });

        //显示
        mPicker.showCityPicker( );

    }

}
